/*
* Copyright 2010 Pavel Kozhin 
*
* Licensed under the Apache License, Version 2.0 (the "License"); 
* you may not use this file except in compliance with the License. 
* You may obtain a copy of the License at 
* 
* http://www.apache.org/licenses/LICENSE-2.0
* 
* Unless required by applicable law or agreed to in writing, software 
* distributed  under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
* See the License for the specific language governing permissions and 
* limitations under the License.
*
* Author: Pavel Kozhin.
*/

package org.etherframework.dependency
{
	import flash.utils.Dictionary;
	
	internal final class DependencyStorage
	{
		private var cache:Dictionary;
		
		public function DependencyStorage(cache:Dictionary)
		{
			this.cache = cache;
		}
		
		public function getItem(value:*):*
		{
			return (value is String) ? cache[value] : null;
		}
		
		public function addItem(value:DependencyInfo):void
		{
			if (!value)
				throw new ArgumentError("Parameter value can not be null.");
			
			if (!value.type)
				throw new Error("DependencyInfo().name should not be null or empty.");
			
			if (!cache[value.type])
			{
				cache[value.type] = value; 
			}
			
			if (value.name && !cache[value.name])
			{
				cache[value.name] = value; 
			}
		}
		
		public function removeItem(value:DependencyInfo):void
		{
			if (!value)
				throw new ArgumentError("Parameter value can not be null.");
			
			if (cache[value.name])
			{
				delete cache[value.name];
			}
			
			if (cache[value.type])
			{
				delete cache[value.type];
			}
		}
	}
}